{extend name='common/_container'} {block name="content"}
<div class="row">
	<div class="col-sm-12">
		<div class="ibox float-e-margins">
			<div class="ibox-title">
				<h5>钥匙管理</h5>
				<button style="float:right; margin-top:-10px;" title="刷新页面" type="button" class="btn btn-default btn-outline" onclick="window.location.reload()" id="">
					<i class="fa fa-refresh"></i>
				</button>
			</div>
			<div class="ibox-content">
				<div class="row row-lg">
					<div class="col-sm-12">
						<div class="row" id="searchGroup">
							<div class="col-sm-2">
								<div class="input-group">
									<div class="input-group-btn">
										<button data-toggle="dropdown" class="btn btn-white dropdown-toggle" type="button">锁名称</button>
									</div>
									<input type="text" class="form-control" id="lock_name" placeholder="锁名称" />
								</div>
							</div>
							<div class="col-sm-2">
								<div class="input-group">
									<div class="input-group-btn">
										<button data-toggle="dropdown" class="btn btn-white dropdown-toggle" type="button">昵称</button>
									</div>
									<input type="text" class="form-control" id="nickname" placeholder="昵称" />
								</div>
							</div>
							<div class="col-sm-2">
								<div class="input-group">
									<div class="input-group-btn">
										<button data-toggle="dropdown" class="btn btn-white dropdown-toggle" type="button">姓名</button>
									</div>
									<input type="text" class="form-control" id="realname" placeholder="姓名" />
								</div>
							</div>
							<div class="col-sm-2">
								<div class="input-group">
									<div class="input-group-btn">
										<button data-toggle="dropdown" class="btn btn-white dropdown-toggle" type="button">手机号</button>
									</div>
									<input type="text" class="form-control" id="mobile" placeholder="手机号" />
								</div>
							</div>
							<div class="col-sm-2">
								<div class="input-group">
									<div class="input-group-btn">
										<button data-toggle="dropdown" class="btn btn-white dropdown-toggle" type="button">钥匙状态</button>
									</div>
									<select class="form-control" id="auth_status">
										<option value="">请选择</option>
										<option value="1">启用</option>
										<option value="0">禁用</option>
									</select>
								</div>
							</div>
							<div class="col-sm-2">
								<div class="input-group">
									<div class="input-group-btn">
										<button data-toggle="dropdown" class="btn btn-white dropdown-toggle" type="button">备注</button>
									</div>
									<input type="text" class="form-control" id="remark" placeholder="备注" />
								</div>
							</div>
							<!-- search end -->
							<div class="col-sm-2">
									<button type="button" class="btn btn-primary " onclick="CodeGoods.search()" id="">
										<i class="fa fa-search"></i>&nbsp;搜索
									</button>
							</div>
						</div>
						<div class="btn-group-sm" id="CodeGoodsTableToolbar" role="group">
						{if condition="in_array('/admin/LockAuth/update',session('admin.nodes')) || session('admin.role') eq 1"}
						<button type="button" id="update" class="btn btn-success button-margin" onclick="CodeGoods.update()">
						<i class="fa fa-pencil"></i>&nbsp;修改
						</button>
						{/if}
						{if condition="in_array('/admin/LockAuth/delete',session('admin.nodes')) || session('admin.role') eq 1"}
						<button type="button" id="delete" class="btn btn-danger button-margin" onclick="CodeGoods.delete()">
						<i class="fa fa-trash"></i>&nbsp;删除
						</button>
						{/if}
						{if condition="in_array('/admin/LockAuth/view',session('admin.nodes')) || session('admin.role') eq 1"}
						<button type="button" id="view" class="btn btn-info button-margin" onclick="CodeGoods.view()">
						<i class="fa fa-plus"></i>&nbsp;查看数据
						</button>
						{/if}
						</div>
						<table id="CodeGoodsTable" data-mobile-responsive="true" data-click-to-select="true">
							<thead><tr><th data-field="selectItem" data-checkbox="true"></th></tr></thead>
						</table>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<script>
	var CodeGoods = {id: "CodeGoodsTable",seItem: null,table: null,layerIndex: -1};

	CodeGoods.initColumn = function () {
 		return [
 			{field: 'selectItem', checkbox: true},
 			{title: '编号', field: 'lockauth_id', visible: true, align: 'center', valign: 'middle',sortable: true},
 			{title: '锁名称', field: 'lock_name', visible: true, align: 'center', valign: 'middle',sortable: true},
 			{title: '头像', field: 'headimgurl', visible: true, align: 'center', valign: 'middle',sortable: true,formatter:CodeGoods.headimgurlFormatter},
 			{title: '昵称', field: 'nickname', visible: true, align: 'center', valign: 'middle',sortable: true},
 			{title: '姓名', field: 'realname', visible: true, align: 'center', valign: 'middle',sortable: true},
 			{title: '手机号', field: 'mobile', visible: true, align: 'center', valign: 'middle',sortable: true},
 			{title: '有效期起始', field: 'auth_starttime', visible: true, align: 'center', valign: 'middle',sortable: true,formatter:CodeGoods.auth_starttimeFormatter},
 			{title: '有效期结束', field: 'auth_endtime', visible: true, align: 'center', valign: 'middle',sortable: true,formatter:CodeGoods.auth_endtimeFormatter},
 			{title: '分享权限', field: 'auth_shareability', visible: true, align: 'center', valign: 'middle',sortable: true,formatter:CodeGoods.auth_shareabilityFormatter},
 			{title: '钥匙状态', field: 'auth_status', visible: true, align: 'center', valign: 'middle',sortable: true,formatter:CodeGoods.auth_statusFormatter},
 			{title: '备注', field: 'remark', visible: true, align: 'center', valign: 'middle',sortable: true},
 			{title: '创建时间', field: 'create_time', visible: true, align: 'center', valign: 'middle',sortable: true,formatter:CodeGoods.create_timeFormatter},
 			{title: '操作', field: '', visible: true, align: 'center', valign: 'middle',formatter: 'CodeGoods.buttonFormatter'},
 		];
 	};

	CodeGoods.buttonFormatter = function(value,row,index) {
		if(row.lockauth_id){
			var str= '';
			{if condition="in_array('/admin/LockAuth/update',session('admin.nodes')) || session('admin.role') eq 1"}
			str += '<button type="button" class="btn btn-success btn-xs" title="修改"  onclick="CodeGoods.update('+row.lockauth_id+')"><i class="fa fa-pencil"></i>&nbsp;修改</button>&nbsp;';
			{/if}
			{if condition="in_array('/admin/LockAuth/delete',session('admin.nodes')) || session('admin.role') eq 1"}
			str += '<button type="button" class="btn btn-danger btn-xs" title="删除"  onclick="CodeGoods.delete('+row.lockauth_id+')"><i class="fa fa-trash"></i>&nbsp;删除</button>&nbsp;';
			{/if}
			return str;
		}
	}

	CodeGoods.headimgurlFormatter = function(value,row,index) {
		if(value){
			return "<a href=\"javascript:void(0)\" onclick=\"openImg('"+value+"')\"><img height='30' src="+value+"></a>";	
		}
	}

	CodeGoods.auth_starttimeFormatter = function(value,row,index) {
		if(value){
			return formatDateTime(value,'Y-m-d H:i:s');	
		}
	}

	CodeGoods.auth_endtimeFormatter = function(value,row,index) {
		if(value){
			return formatDateTime(value,'Y-m-d H:i:s');	
		}
	}

	CodeGoods.auth_shareabilityFormatter = function(value,row,index) {
		if(value !== null){
			if(value == 1){
				return '<input class="mui-switch mui-switch-animbg auth_shareability'+row.lockauth_id+'" type="checkbox" onclick="CodeGoods.updateauth_shareability('+row.lockauth_id+',0,\'auth_shareability\')" checked>';
			}else{
				return '<input class="mui-switch mui-switch-animbg auth_shareability'+row.lockauth_id+'" type="checkbox" onclick="CodeGoods.updateauth_shareability('+row.lockauth_id+',1,\'auth_shareability\')">';
			}
		}
	}


	CodeGoods.updateauth_shareability = function(pk,value,field) {
		var ajax = new $ax(Feng.ctxPath + "/LockAuth/updateExt", function (data) {
			if ('00' !== data.status) {
				Feng.error(data.msg);
				$("."+field+pk).prop("checked",!$("."+field+pk).prop("checked"));
			}
		});
		var val = $("."+field+pk).prop("checked") ? 1 : 0;
		ajax.set('lockauth_id', pk);
		ajax.set('auth_shareability', val);
		ajax.start();
	}

	CodeGoods.auth_statusFormatter = function(value,row,index) {
		if(value !== null){
			if(value == 1){
				return '<input class="mui-switch mui-switch-animbg auth_status'+row.lockauth_id+'" type="checkbox" onclick="CodeGoods.updateauth_status('+row.lockauth_id+',0,\'auth_status\')" checked>';
			}else{
				return '<input class="mui-switch mui-switch-animbg auth_status'+row.lockauth_id+'" type="checkbox" onclick="CodeGoods.updateauth_status('+row.lockauth_id+',1,\'auth_status\')">';
			}
		}
	}


	CodeGoods.updateauth_status = function(pk,value,field) {
		var ajax = new $ax(Feng.ctxPath + "/LockAuth/updateExt", function (data) {
			if ('00' !== data.status) {
				Feng.error(data.msg);
				$("."+field+pk).prop("checked",!$("."+field+pk).prop("checked"));
			}
		});
		var val = $("."+field+pk).prop("checked") ? 1 : 0;
		ajax.set('lockauth_id', pk);
		ajax.set('auth_status', val);
		ajax.start();
	}

	CodeGoods.create_timeFormatter = function(value,row,index) {
		if(value){
			return formatDateTime(value,'Y-m-d H:i:s');	
		}
	}

	CodeGoods.formParams = function() {
		var queryData = {};
		queryData['lock_name'] = $("#lock_name").val();
		queryData['nickname'] = $("#nickname").val();
		queryData['realname'] = $("#realname").val();
		queryData['mobile'] = $("#mobile").val();
		queryData['auth_status'] = $("#auth_status").val();
		queryData['remark'] = $("#remark").val();
		queryData['user_id'] = $("#user_id").val();
		return queryData;
	}

	CodeGoods.check = function () {
		var selected = $('#' + this.id).bootstrapTable('getSelections');
		if(selected.length == 0){
			Feng.info("请先选中表格中的某一记录！");
			return false;
		}else{
			CodeGoods.seItem = selected;
			return true;
		}
	};

	CodeGoods.add = function (value) {
		var url = location.search;
		var index = layer.open({type: 2,title: '添加',area: ['800px', '100%'],fix: false, maxmin: true,content: Feng.ctxPath + '/LockAuth/add'+url});
		this.layerIndex = index;
		if(!IsPC()){layer.full(index)}
	}


	CodeGoods.update = function (value) {
		if(value){
			var index = layer.open({type: 2,title: '修改',area: ['800px', '450px'],fix: false, maxmin: true,content: Feng.ctxPath + '/LockAuth/update?lockauth_id='+value});
			if(!IsPC()){layer.full(index)}
		}else{
			if (this.check()) {
				var idx = '';
				$.each(CodeGoods.seItem, function() {
					idx += ',' + this.lockauth_id;
				});
				idx = idx.substr(1);
				if(idx.indexOf(",") !== -1){
					Feng.info("请选择单条数据！");
					return false;
				}
				var index = layer.open({type: 2,title: '修改',area: ['800px', '450px'],fix: false, maxmin: true,content: Feng.ctxPath + '/LockAuth/update?lockauth_id='+idx});
				this.layerIndex = index;
				if(!IsPC()){layer.full(index)}
			}
		}
	}


	CodeGoods.delete = function (value) {
		if(value){
			Feng.confirm("是否删除选中项？", function () {
				var ajax = new $ax(Feng.ctxPath + "/LockAuth/delete", function (data) {
					if ('00' === data.status) {
						Feng.success(data.msg);
						CodeGoods.table.refresh();
					} else {
						Feng.error(data.msg);
					}
				});
				ajax.set('lockauth_ids', value);
				ajax.start();
			});
		}else{
			if (this.check()) {
				var idx = '';
				$.each(CodeGoods.seItem, function() {
					idx += ',' + this.lockauth_id;
				});
				idx = idx.substr(1);
				Feng.confirm("是否删除选中项？", function () {
					var ajax = new $ax(Feng.ctxPath + "/LockAuth/delete", function (data) {
						if ('00' === data.status) {
							Feng.success(data.msg);
							CodeGoods.table.refresh();
						} else {
							Feng.error(data.msg);
						}
					});
					ajax.set('lockauth_ids', idx);
					ajax.start();
				});
			}
		}
	}


	CodeGoods.view = function (value) {
		if(value){
			var index = layer.open({type: 2,title: '查看数据',area: ['800px', '100%'],fix: false, maxmin: true,content: Feng.ctxPath + '/LockAuth/view?lockauth_id='+value});
			if(!IsPC()){layer.full(index)}
		}else{
			if (this.check()) {
				var idx = '';
				$.each(CodeGoods.seItem, function() {
					idx += ',' + this.lockauth_id;
				});
				idx = idx.substr(1);
				var index = layer.open({type: 2,title: '查看数据',area: ['800px', '100%'],fix: false, maxmin: true,content: Feng.ctxPath + '/LockAuth/view?lockauth_id='+idx});
				this.layerIndex = index;
				if(!IsPC()){layer.full(index)}
			}
		}
	}


	CodeGoods.search = function() {
		CodeGoods.table.refresh({query : CodeGoods.formParams()});
	};

	$(function() {
		var defaultColunms = CodeGoods.initColumn();
		var url = location.search;
		var table = new BSTable(CodeGoods.id, Feng.ctxPath+"/LockAuth/index"+url,defaultColunms,20);
		table.setPaginationType("server");
		table.setQueryParams(CodeGoods.formParams());
		CodeGoods.table = table.init();
	});
</script>
{/block}